require "Cocos2d"
require "Cocos2dConstants"

function AddSearchPath(path)
    if path then
        if cc.Application:getInstance():getTargetPlatform() == cc.PLATFORM_OS_WINDOWS then
            --cc.FileUtils:getInstance():addSearchPath("../../../svn/mingyue/stg/src/encrypt/"..path);
            
            cc.FileUtils:getInstance():addSearchPath("../../../Last/svn/mingyue/stg/src/"..path);
            
            cc.FileUtils:getInstance():addSearchPath("../../../svn/mingyue/stg/src/"..path);
        end
        cc.FileUtils:getInstance():addSearchPath(path)

        --writable path
        local writablePath = cc.FileUtils:getInstance():getWritablePath();
        cc.FileUtils:getInstance():addSearchPath(writablePath..path);
    end
end

function Start()
    require "AudioEngine"
    
    require "const"
   
    cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(CONST_GAME_WIDTH,CONST_GAME_HEIGHT, cc.ResolutionPolicy.SHOW_ALL)
    
    AddSearchPath("data/excel")
    AddSearchPath("data/lua")
    AddSearchPath("data/rsc")
    AddSearchPath("data/rsc/UI")

    local W = 11;
    local H = 7;
    local wu = CONST_GAME_WIDTH/W;
    local hu = CONST_GAME_HEIGHT/H;
    for i=0,W+1 do
        for j=1,H+1 do
            local ch = string.char(string.byte('a') + j-1);  
            local name = ch..i;
            _G[name] = {wu*i-wu/2, hu*(H-j+1)-hu/2}
        end
        
        local j = 0;
        local name = "z"..i;
        _G[name] = {wu*i-wu/2, hu*(H-j+1)-hu/2}
    end

    math.randomseed(os.time()) 
    
    require "requires"

    cc.Director:getInstance():setDisplayStats(EXCEL.const["ifDisplayStats"]);
    cc.Director:getInstance():setAnimationInterval(EXCEL.const["animationInterval"])
    
    ReadConfigData("comLevel", nil, -1);
    ReadConfigData("comStar", nil, -1);
    ReadConfigData("comValue", nil, -1);
    for k,v in pairs(EXCEL.item) do
        ReadConfigData("items", k, -1);
    end
    for k,v in pairs(EXCEL.equip) do
        ReadConfigData("equips", k, -1);
    end
    ReadConfigData("petOn", nil, 0);
    for k,v in pairs(EXCEL.pet) do
        ReadConfigData("pets", k, -1);
    end
    
    if g_Player.comLevel<=0 or g_Player.comLevel>#EXCEL.com then
        SetConfigData("comLevel", nil, 1);
    end
    if g_Player.comStar<=0 or g_Player.comStar>EXCEL.com[#EXCEL.com].star then
        SetConfigData("comStar", nil, 0);
    end
    if g_Player.comValue<=0 or g_Player.comValue>EXCEL.com[#EXCEL.com].starValue then
        SetConfigData("comValue", nil, 0);
    end
    
    for k,v in pairs(EXCEL.item) do
        if g_Player.items[k]<=0 then
            SetConfigData("items", k, 0);
        end
        if g_Player.items[k]>=CONST.maxUint then
            SetConfigData("items", k, 0);
        end
    end
    
    for k,v in pairs(EXCEL.item) do
        if g_Player.items[k]<=9999 then
            SetConfigData("items", k, k<100 and 9999 or 999);
        end
    end
    
    for k,v in pairs(EXCEL.equip) do
        if g_Player.equips[k]<=1 or g_Player.equips[k]>=CONST.maxUint then
            SetConfigData("equips", k, 1);
        end
    end
    
    for k,v in pairs(EXCEL.pet) do
        if g_Player.pets[k]<0 then
            SetConfigData("pets", k, 0);
        end
    end
    if not EXCEL.pet[g_Player.petOn] then
        SetConfigData("petOn", nil, 0);
    end
    
    print(cc.UserDefault:getXMLFilePath())
    
    local testGateId = EXCEL.const["test"];
    if IsNewbie() then
        if IsNewbie() then
            local d = cc.UserDefault:getInstance();
            d:setBoolForKey("newbie",true)
            d:flush();
        end
        EnterGate(EXCEL.const["newbieGate"])
    elseif EXCEL.gate[testGateId] then
        EnterGate(testGateId);
    else
        EnterLogin();
    end
    
    return true;
end


